package com.unionftech.crm_android.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.KeyEvent
import android.widget.ImageView
import com.unionftech.baselibrary.glide.loadImage
import com.unionftech.common.aspectj.LoginFilter
import com.unionftech.common.base.BaseActivity
import com.unionftech.common.base.mvp.annitation.InjectPresenter
import com.unionftech.common.base.mvp.impl.BaseMvpActivity
import com.unionftech.common.bean.ResponseResult
import com.unionftech.common.common.GlobalInfoManager
import com.unionftech.common.common.bean.BannerImageInfo
import com.unionftech.common.common.bean.BannerPosition
import com.unionftech.common.common.contract.CommonContract
import com.unionftech.common.common.presenter.CommonPresenter
import com.unionftech.common.utils.LogUtils
import com.unionftech.crm_android.R


class UFAdStartActivity : BaseMvpActivity(),
    CommonContract.IBannerView {
    private val mIvNetImg by lazy { findViewById<ImageView>(R.id.iv_net_img) }
    private val mMainHandler = Handler(Looper.getMainLooper())

    @InjectPresenter
    private lateinit var mCommonPresenter: CommonPresenter

    companion object {
        private const val DELAY_TIME = 3000L
        private const val TAG = "UFAdStartActivity"

        @JvmStatic
        fun startThis(context: Context) {
            val intent = Intent(context, UFAdStartActivity::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mMainHandler.postDelayed({
            gotoNext()
        }, DELAY_TIME)
    }

    override fun initLayout() = R.layout.activity_ufad_start

    override fun statusBar() {
        fullScreen()
    }

    override fun setView() {
        super.setView()
        loadData()
        hideTitleBar()
    }

    override fun loadData() {
        super.loadData()
        val imageInfos = GlobalInfoManager.getInstance().getBannerInfo(BannerPosition.AD)
        if (imageInfos.isNullOrEmpty()) {
            mCommonPresenter.getBannerList(BannerPosition.AD)
        } else {
            updateAdImage(imageInfos)
        }
    }

    override fun needShowLoading(): Boolean {
        return false
    }

    @LoginFilter
    private fun gotoNext() {
        UFMainStandardActivity.startThis(this)
        finish()
    }

    /**
     * 屏蔽物理返回键
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            true
        } else super.onKeyDown(keyCode, event)
    }

    override fun handleBannerView(
        position: BannerPosition,
        response: ResponseResult<List<BannerImageInfo>>
    ) {
        if (response.code == ResponseResult.SUCCESS_CODE) {
            response.data?.also { list ->
                if (position == BannerPosition.AD) {
                    updateAdImage(list)
                }
            }
        } else {
            LogUtils.d(TAG, "handleBannerView error->$response")
        }
    }

    private fun updateAdImage(list: List<BannerImageInfo>) {
        list.getOrNull(0)?.also {
            mIvNetImg.loadImage(it.imageUrl)
        }
    }
}